import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { createElement } from 'react';
import dynamic from 'dva/dynamic';
import { getConvertRouter } from 'cpaas-front/lib/utils/getConvertRouter';
import routers from "../config/routers"; // import { isString } from 'lodash';

import "../customize"; // import routers from '../../config/routers';

var modelNotExisted = function modelNotExisted(app, model) {
  return (// eslint-disable-next-line
    !app._models.some(function (_ref) {
      var namespace = _ref.namespace;
      return namespace === model.substring(model.lastIndexOf('/') + 1);
    })
  );
}; // function convertRouter(nestRoutes, options = {}) {
//   const { app } = options;
//   const convertRoutes = {};
//   unWindRouteToRouter(nestRoutes, convertRoutes, { app });
//   return convertRoutes;
// }
// function unWindRouteToRouter(nestRoutes, convertRoutes, options = {}) {
//   const { app } = options;
//   nestRoutes.forEach(route => {
//     const newRoute = {};
//     assignRouterData(newRoute, route, { app });
//     // eslint-disable-next-line no-param-reassign
//     convertRoutes[route.path] = newRoute;
//     // 子路由
//     if (route.components !== undefined) {
//       unWindRouteToRouter(route.components, convertRoutes, options);
//     }
//   });
// }
// function assignRouterData(router, route, options = {}) {
//   const { app } = options;
//   const models = route.models === undefined ? [] : route.models;
//   if (route.component === undefined) {
//     // eslint-disable-next-line no-param-reassign
//     router.component = dynamicWrapper(app, models, () => import('../routes'));
//   } else if (isString(route.component)) {
//     // eslint-disable-next-line no-param-reassign
//     router.component = dynamicWrapper(app, models, () => import(`../routes/${route.component}`));
//   } else {
//     // eslint-disable-next-line no-param-reassign
//     router.component = route.component;
//   }
//   if (route.key !== undefined) {
//     // eslint-disable-next-line no-param-reassign
//     router.key = route.key;
//   }
//   if (route.authorized !== undefined) {
//     // eslint-disable-next-line no-param-reassign
//     router.authorized = route.authorized;
//   }
//   if (route.title !== undefined) {
//     // eslint-disable-next-line no-param-reassign
//     router.title = route.title;
//   }
//   if (route.icon !== undefined) {
//     // eslint-disable-next-line no-param-reassign
//     router.icon = route.icon;
//   }
// }
// // wrapper of dynamic


export var dynamicWrapper = function dynamicWrapper(app, _models, _component) {
  return dynamic({
    app: app,
    models: function models() {
      return _models.filter(function (model) {
        return modelNotExisted(app, model);
      }).map(function (m) {
        return import("../models/".concat(m, ".js"));
      });
    },
    // add routerData prop
    component: function component() {
      return (// if (!routerDataCache) {
        //   routerDataCache = getRouterData(app);
        // }
        _component().then(function (raw) {
          var Component = raw.default || raw;
          return function (props) {
            return createElement(Component, _objectSpread({}, props));
          };
        })
      );
    }
  });
}; // export function getRouterData(app) {
//   return {
//     '/': {
//       component: dynamicWrapper(app, ['user', 'login'], () => import('../layouts/DefaultLayout')),
//     },
//     ...convertRouter(routers || [], { app }),
//   };
// }

var convertRouter = function convertRouter(app) {
  return getConvertRouter({
    cpaasRoutes: routers,
    options: {
      app: app
    }
  });
};

export function getRouterData(app) {
  return convertRouter(app)();
}